package dcds2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;

/**
 *
 * @author Mesan
 */
public class EditProcedureWindow extends javax.swing.JFrame {

   public int row1 = 0;
   public int procedureID1 = 0;
    public EditProcedureWindow(int row, int procedureID) {
            row1 = row;
            procedureID1 = procedureID;
            int errCtr = 0;
            
            initComponents();
            setLocationRelativeTo(null);

           //GET SELECTED ITEM FROM MAIN WINDOW AND PRINT IN TEXTBOX
           
    try{
        
        ConnectionFactory myFactory = ConnectionFactory.getFactory();
        Connection conn = myFactory.getConnection();
        boolean result = false;
        
        PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.procedures WHERE procedureID = " +procedureID);
        ResultSet rs = pstmt.executeQuery();
        
        while(rs.next()){
             
                String name = rs.getString("procedureName");            
                txtProcedureName.setText(name);                    // PRINT IN EDIT PROCEDURE TEXT FIELD
                String price = rs.getString("procedurePrice");
                txtProcedurePrice.setText(price);
            }
     
       // int g = stat.executeUpdate("UPDATE dcds.procedures SET procedureName = " +nName+ "procedurePrice = "+pP+" "
       //         + "WHERE procedureID = "+(b+1));

        conn.close();
        result = true;
    }catch(Exception e){
        e.printStackTrace();
        }   
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtProcedureName = new javax.swing.JTextField();
        txtProcedurePrice = new javax.swing.JTextField();
        btnConfirm = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        ToggleEdit = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("DCDS - View/Edit Procedure");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setFont(new java.awt.Font("HelveticaNeueLT Std Med", 1, 14));
        jLabel1.setText("Procedure Name:");

        jLabel2.setFont(new java.awt.Font("HelveticaNeueLT Std Med", 1, 14));
        jLabel2.setText("Price:");

        txtProcedureName.setEditable(false);
        txtProcedureName.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        txtProcedurePrice.setEditable(false);
        txtProcedurePrice.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        txtProcedurePrice.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtProcedurePriceKeyTyped(evt);
            }
        });

        btnConfirm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonConfirm.png"))); // NOI18N
        btnConfirm.setEnabled(false);
        btnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmActionPerformed(evt);
            }
        });

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonBack.png"))); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        ToggleEdit.setFont(new java.awt.Font("HelveticaNeueLT Std Lt Cn", 1, 12));
        ToggleEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonEdit.png"))); // NOI18N
        ToggleEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ToggleEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ToggleEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
                        .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtProcedurePrice, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtProcedureName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtProcedureName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtProcedurePrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(ToggleEdit))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        if (btnCancel.getIcon().toString().contains("images/module/ButtonBack.png")) {
            dispose();
            new MainWindow(2).setVisible(true);
        }
        if (btnCancel.getIcon().toString().contains("images/module/ButtonCancel.png")) {
            btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                "/images/module/ButtonBack.png")));
            txtProcedureName.setEditable(false);
            txtProcedurePrice.setEditable(false);
            ToggleEdit.setSelected(false);
            btnConfirm.setEnabled(false);
        }
}//GEN-LAST:event_btnCancelActionPerformed

private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmActionPerformed

     JFrame prompt = new JFrame();
     int selection = JOptionPane.showConfirmDialog(prompt, "Are all the fields correct?", "Confirmation", JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE);
     int errCtr = 0;
     
     if(selection == JOptionPane.YES_OPTION){
     
     String nName = txtProcedureName.getText();
     String nPrice = txtProcedurePrice.getText();
     
          try{
              double pp = Integer.parseInt(txtProcedurePrice.getText());
              }
              catch (NumberFormatException nfe){
                  errCtr++;
                JOptionPane.showMessageDialog(prompt, "Please enter a valid price!","NOTICE!",JOptionPane.PLAIN_MESSAGE);  
              }
     
          //ERROR CHECKING//
     try{
             ConnectionFactory myFactory = ConnectionFactory.getFactory();
             Connection conn = myFactory.getConnection();
             
             boolean result = false;
            
             PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.procedures");
             ResultSet rs = pstmt.executeQuery();
             
         while(rs.next()){
             
             String checkName = rs.getString("procedureName");
             String checkPrice = rs.getString("procedurePrice");
             
        // CHECKS IF THERE IS INPUT IN PROCEDURE NAME TEXTBOX 
         if(txtProcedureName.getText().length() == 0){
                   errCtr++;
                   JOptionPane.showMessageDialog(prompt, "Please enter a procedure name!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
         
         //CHECK IF THERE IS AN INPUT IN PROCEDURE PRICE TEXTBOX
        else if(txtProcedurePrice.getText().length() == 0){
                   errCtr++;
                   JOptionPane.showMessageDialog(prompt, "Please enter a price!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
       
        // CHECKS IF THERE ARE NO CHANGES MADE IN THE DATABASE
       else if(txtProcedureName.getText().compareToIgnoreCase(checkName)==0 && txtProcedurePrice.getText().compareToIgnoreCase(checkPrice)==0){
         
         JOptionPane.showConfirmDialog(prompt, "There were no changes made to the procedure!", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
         errCtr++;
            }
         }
         }catch(Exception ex){ ex.printStackTrace(); }
     
        //ERROR CHECKING//
           
              if(errCtr == 0){
              try{
                 
                  ConnectionFactory myFactory = ConnectionFactory.getFactory();
                  Connection conn = myFactory.getConnection();
                  boolean result = false;
                  
                  PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.procedures");
                  ResultSet rs = pstmt.executeQuery();

                  int g = pstmt.executeUpdate("UPDATE dcds.procedures SET procedureName = \"" +nName+ "\",procedurePrice = \"" +nPrice+ "\"WHERE procedureID = \""+procedureID1+"\"");
             
                  conn.close();
                  result = true;
             
         }catch(Exception e){
             e.printStackTrace();
         }
             JOptionPane.showMessageDialog(prompt, "You have successfully edited the procedure!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                  
        dispose();
        new MainWindow(2).setVisible(true);
        }
              else if(errCtr > 0){
                  errCtr = 0;
              }
   }
}//GEN-LAST:event_btnConfirmActionPerformed

private void ToggleEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ToggleEditActionPerformed
    btnConfirm.setEnabled(true);
    txtProcedureName.setEditable(true);
    txtProcedurePrice.setEditable(true);
    btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource(
            "/images/module/ButtonCancel.png")));
}//GEN-LAST:event_ToggleEditActionPerformed

private void txtProcedurePriceKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtProcedurePriceKeyTyped
    if (evt.getKeyChar() == java.awt.event.KeyEvent.VK_ENTER) {
        btnConfirm.doClick();
    }
}//GEN-LAST:event_txtProcedurePriceKeyTyped

    /**
    * @param args the command line arguments
    */
  /*  public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditProcedureWindow().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton ToggleEdit;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnConfirm;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtProcedureName;
    private javax.swing.JTextField txtProcedurePrice;
    // End of variables declaration//GEN-END:variables

    private Connection connection;
    private Statement stat;
    private ResultSet rs;

public String oldName(){

    
    return "asdf";
}    


}
